<html>
<head>
    <meta charset="UTF-8">
    <title>绑定</title>
    <link rel="stylesheet" href="../css/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/vue.min.js"></script>
    <script src="../js/axios.min.js"></script>
    <script src="../js/index.js"></script>
</head>
<body style="text-align: center;">
<el-container id="app" style="height: 350px;">
    <div style="width: 400px;">
        <h3>账号绑定</h3>
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="已有账号" name="first">
                <el-form :model="ruleForm1" status-icon :rules="rules1" ref="ruleForm1">
                    <el-form-item prop="name">
                        <el-input type="text" prefix-icon="el-icon-user" v-model="ruleForm1.name" autocomplete="off"
                                  placeholder="请输入账号"></el-input>
                    </el-form-item>
                    <el-form-item prop="pwd">
                        <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm1.pwd" autocomplete="off"
                                  placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm1('ruleForm1')">绑定</el-button>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane label="注册新账号" name="second">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
                    <el-form-item  prop="pwd">
                        <el-input type="password" v-model="ruleForm.pwd" prefix-icon="el-icon-lock" autocomplete="off" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" prefix-icon="el-icon-lock" autocomplete="off" placeholder="请再次输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">绑定</el-button>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
        </el-tabs>
    </div>
</el-container>
</body>
<script>
    new Vue({
        el: '#app',
        data() {
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pwd) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                activeName: 'first',
                ruleForm1: {
                    name: '',
                    pwd: '',
                },
                ruleForm: {
                    name: '1237',
                    pwd: '',
                    checkPass: '',
                },
                rules: {
                    name: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    pwd: [
                        {required: true, validator: validatePass, trigger: 'blur'},
                        {min: 6, message: '长度至少 6 个字符', trigger: 'blur'}
                    ],
                    checkPass: [
                        {required: true, validator: validatePass2, trigger: 'blur'}
                    ]
                },
                rules1: {
                    name1: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                    ],
                    pwd1: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            handleClick(tab, event) {
                console.log(tab, event);
            },
            submitForm1(formName1) {
                let here = this;
                this.$refs[formName1].validate((valid) => {
                    if (valid) {
                        let form = encodeURIComponent(JSON.stringify(this.ruleForm1));
                        axios({
                            method: 'post',
                            url: '/dao/update/connect',
                            data: form
                        }).then(function (response) {
                            let data = JSON.parse(decodeURIComponent(response.data));
                            if (data.success === "1") {
                                here.$message({
                                    message: '恭喜你，绑定成功！',
                                    type: 'success'
                                });
                                window.setTimeout("window.location='http://localhost:8080/index.html'", 1000);
                            } else {
                                here.$message.error('账号或密码错误！');
                            }
                        }).catch(function (error) {
                            if (error.response) {
                                here.$message.error('服务器正在开小差');
                            } else {
                                console.log('Error', error.message);
                            }
                            console.log(error.config);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            submitForm(formName) {
                let here = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let f = {
                            name: this.ruleForm.name,
                            pwd: this.ruleForm.pwd
                        };
                        let form = encodeURIComponent(JSON.stringify(f));
                        axios({
                            method: 'post',
                            url: '/dao/update/signUp',
                            data: form
                        }).then(function(response) {
                            let data = JSON.parse(decodeURIComponent(response.data));
                            if(data.success === "1") {
                                here.$message({
                                    message: '恭喜你，绑定成功！',
                                    type: 'success'
                                });
                                window.setTimeout("window.location='http://localhost:8080/index.html'",1000);
                            } else {
                                here.$message.error("注册失败");
                            }
                        }).catch(function (error) {
                            if (error.response) {
                                here.$message.error('服务器正在开小差');
                            } else {
                                console.log('Error', error.message);
                            }
                            console.log(error.config);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    });
</script>
</html>

